#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import base64
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad

class Crypto:
    @staticmethod
    def aes_encrypt(data: str, key: str) -> str:
        """AES加密"""
        key = key.encode('utf-8')
        key = pad(key, AES.block_size)
        cipher = AES.new(key, AES.MODE_CBC)
        ct_bytes = cipher.encrypt(pad(data.encode('utf-8'), AES.block_size))
        iv = base64.b64encode(cipher.iv).decode('utf-8')
        ct = base64.b64encode(ct_bytes).decode('utf-8')
        return f"{iv}:{ct}"
    
    @staticmethod
    def aes_decrypt(data: str, key: str) -> str:
        """AES解密"""
        key = key.encode('utf-8')
        key = pad(key, AES.block_size)
        iv, ct = data.split(':')
        iv = base64.b64decode(iv)
        ct = base64.b64decode(ct)
        cipher = AES.new(key, AES.MODE_CBC, iv)
        pt = unpad(cipher.decrypt(ct), AES.block_size)
        return pt.decode('utf-8')
    
    @staticmethod
    def md5(data: str) -> str:
        """计算MD5"""
        import hashlib
        return hashlib.md5(data.encode('utf-8')).hexdigest()
    
    @staticmethod
    def sha1(data: str) -> str:
        """计算SHA1"""
        import hashlib
        return hashlib.sha1(data.encode('utf-8')).hexdigest() 